//一般直接写在一个js文件中
layui.use(['layer', 'form'], function () {
    var layer = layui.layer
        , form = layui.form;


    form.verify({
        repassword: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (!/^[\S]{6,12}$/.test(value)) {
                return '重复密码要到6-12位，不能有空格';
            }
            let pass = $("[type='password']").val();
            console.log(pass)
            if (value != pass) {
                return '二次密码不一致';
            }
        }
        , password: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格']
    });


    form.on('submit(registerFrom)', function (data) {
        //弹出正在加载；
        // var loading = layer.load(1, {
        //     shade: [0.6, "black"]
        // });

        $.ajax({
            url: "/getEncode",
            data: "token=" + data.field.token,
            type: "GET",
            dataType: "JSON",
            success: function (res) {
                if (res.code) {
                    let code = res.code;
                    let password = $.base64.encode(data.field.password + "-" + code);
                    let repassword = $.base64.encode(data.field.repassword + "-" + code);
                    // //同步请求
                    // $(data.form).find("[name='password']").val(password)
                    // $(data.form).find("[name='repassword']").val(repassword)
                    //异步请求
                    data.field.password = password;
                    data.field.repassword = repassword;
                    $.ajax({
                        url: "/user/registerJson",
                        data: data.field,
                        type: "POST",
                        dataType: "JSON",
                        success: function (rs) {
                            if (rs.success()) {
                                layer.msg(rs.content, {
                                    end: function () {
                                        window.location = "/client/login.jsp"
                                    }
                                })
                            } else {
                                layer.msg(rs.content)
                            }
                        }, error: function () {
                            layer.msg("服务器异常")
                        }, compact: function () {
                            layer.close(loading);
                        }
                    });

                } else {
                    layer.msg("非法", {
                        end: function () {
                            window.location = "/client/register.jsp";
                        }
                    })
                }
            }, error: function () {
                layer.close(loading);
                layer.msg("服务器异常")
            }
        });

        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });


});




